<?php
namespace app\ausadmin\controller;
use app\ausadmin\controller\Common;
use think\Request;
use think\Db;
use think\File;
use think\Session;
use think\Cookie;
use Godok\Org\FileManager;
use Qcloud\cos\Api;
class Jysetting extends Common
{
	public function index(){
		$where['is_delete'] =  1;
		$where['id'] = 1;//管理后台 直营的oid = 1
		$query= Db::name( 'operator')
                ->where($where)
                ->find();
        if(!$query){
        	return $this->fetch('add');
        }
        $where2['oid'] = $where['id'];
        $agreement = Db::name( 'agreement')
        			->where($where2)
        			->find();		
        $this->assign('base',$query);  
        $this->assign('xy',$agreement);    
		return $this->fetch();
	}
	//基本信息更新
    /**
     *基本信息的更新
     *
     */
	public function edit(){
		if (Request::instance()->isPost() ){
			$data = input('post.');
			if($_FILES['logo']['name']){
				$file = Request::instance()->file('logo');
				$info=$file->move(ROOT_PATH . 'public' . DS . 'uploads');
				$file = $info->getSaveName();
				if($file){
                    $cosApi = new Api(config('Qcloud'));
                    $bucket = config('Qcloud.bucket');
                    $src = ROOT_PATH . 'public/uploads/'.$file;
                    $dst = 'operator/logo/'. $file;
                    $ret = $cosApi->upload($bucket, $src, $dst);
					if($ret['code']==0){
	                    $path = $ret['data']['resource_path'];  //获取资源文件路径（包含了APPID和bucketname）
	                    $len = strlen($bucket);  //计算bucketname长度
	                    $start = strpos($path,$bucket);   //计算bucketname第一次出现的问题
	                    $start +=$len;  
	                    $newpath = substr($path,$start);  //截取路径
	                    unlink($src);  //删除服务器上面的资源文件
	                    $res['code'] = '1';
	                    $res['picture'] = $newpath;  //资源文件路径（需要前面添加上域名地址才可以使用）
	                    $res['url'] = $ret['data']['access_url'];   //资源文件URL
	                    $data['logo'] = $res['picture'];
	                }   
                }
			}
	         if($data['name']=='' || $data['contacts']=='' || $data['mobile']=='' || $data['addrs']=='' ){
	         	$this->error('字段不能有空值');
	         }
	         $where['name'] = $data['name'];
	         $where['id'] = '<>'.$data['id'];
	         $check = Db::name('operator')->where($where)->find();
	         if(!$check){
	         	if(Db::name( 'operator')->where('id',$data['id'])->update($data) !==false){
	         		$oid = 1;$m = 'ausadmin'; $c = 'Jysetting'; $action="edit"; $uid=session('adminid'); $querystring = '更新经营设置的基本信息';
			        $ip = get_ip();$os = get_os();
			        set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
			        return ['code'=>0, 'msg'=>lang('更新成功！')];
	    		}else{
	    			return ['code'=>-1050, 'msg'=>lang('更新失败！')];
	    		}
	    		return;
		    }else{
		    	$this->error('运营名称重名！');

		    }
	    }

	}
	//营销信息更新
	public function yxinfo(){
		if (Request::instance()->isPost() ){
			$prepaidamount = input('post.prepaidamount/a',[]);
			$s2 = implode('|',$prepaidamount);
			$data['id'] = input('id');
			$data['prepaidamount'] = $s2;
			$data['deposit'] = input('deposit');
			$data['deposit_delay'] = input('deposit_delay');
			$data['billingtime'] = input('billingtime');
			$data['amount'] = input('amount');
			$data['billingunit'] = input('billingunit');
			$data['freetime'] = input('freetime');
			$data['ceiling'] = input('ceiling');
			if(Db::name( 'operator')->update($data) !==false){
					$oid = 1;$m = 'ausadmin'; $c = 'Jysetting'; $action="yxinfo"; $uid=session('adminid'); $querystring = '更新经营设置的营销信息';
			        $ip = get_ip();$os = get_os();
			        set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
			        return ['code'=>0, 'msg'=>lang('更新成功！')];
	    			//$this->redirect('index');
	    		}else{
	    			return ['code'=>-1050, 'msg'=>lang('更新失败！')];
	    			//$this->error('修改失败！');
	    		}
	    		return;
		}

	}
	//协议内容更新
    /**
     *更新协议
     *
     */
	public function agreement(){

		if (Request::instance()->isPost() ){
			$data = input('post.');
			$where = ['oid' => 1];
		    $check = Db::name('agreement')->field('id')->where($where)	->find();
		    if (!$check) {
		        $data['oid'] = 1;
		        $res = Db::name('agreement')->insert($data);
		        if ($res) {
                    return ['code'=>0, 'msg'=>lang('更新成功！')];
                }
            }
			if(Db::name( 'agreement')->where($where)->update($data)){
					$oid = 1;$m = 'ausadmin'; $c = 'Jysetting'; $action="agreement"; $uid=session('adminid'); $querystring = '更新经营设置的协议内容';
			        $ip = get_ip();$os = get_os();
			        set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
			        return ['code'=>0, 'msg'=>lang('更新成功！')];
	    		}else{
	    			return ['code'=>-1050, 'msg'=>lang('更新失败！')];
	    		}
	    	return;
		}
	}
	//支付信息
	public function paysetting(){
		$where['oid'] = 1;
		$pay = Db::name( 'wechatpay')
                ->where($where)
                ->find();
		if($pay) {
		    $pay['vue_key'] = json_encode([['name' => $pay['sslkey_path']]]);
		    $pay['vue_cert'] = json_encode([['name' => $pay['sslcert_path']]]);
        }
        $this->assign('info',$pay);

        if (Request::instance()->isPost()) {
            $id = 1;
            $payId = input('id', 0, 'intval');

            $data['sslcert_path'] = input('sslcert_path');
            $data['sslkey_path'] = input('sslkey_path');
            $data['oid'] = $id;
            $data['mchid'] = input('mchid', '', 'trim');
            $data['mchkey'] = input('mchkey', '', 'trim');
            $data['app_key'] = input('app_key', '', 'trim');
            $validate = \think\Loader::validate('Wechatpay');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }

            if ($payId) {//编辑
               $res = Db::name('wechatpay')->where(['id' => $payId])->update($data);
            } else {//添加
                $res = Db::name('wechatpay')->insert($data);
            }
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        return $this->fetch('addpay');
	}

	public function file_upload()
    {
        $id = 1;
        $path = '../extend/wxpay/cert/'.$id.'/';
        $name = Request::instance()->get('name');
        if($_FILES[$name]['name']){
            $file1 = Request::instance()->file($name);
            $file1_info = $file1->move($path,'');
            $file1_info = $path.$file1_info->getSaveName();
            $info = ['name' => $name, 'file' => $file1_info];
            die(json_encode($info));
        } else {
            die();
        }
    }

	public function zfbpay(){
		$this->redirect('paysetting');
	}

	public function smssetting(){

		$where['oid'] = 1;
		//$where['oid'] = 2;
		$query= Db::name( 'verificationcode')
                ->where($where)
                ->find();
        if(!$query){
        	return $this->fetch('smsadd');
        }        
         $this ->assign('dx',$query);      	
         if (Request::instance()->isPost() ){

         	$data = input('post.');
	    	$validate = \think\Loader::validate('Verificationcode'); 
    		if(!$validate->scene('smssetting')->check($data)){
                $this->error($validate->getError());
            }
            if(Db::name('verificationcode')->update($data) !==false){
            	$oid = 1;$m = 'ausadmin'; $c = 'Jysetting'; $action="smssetting"; $uid=session('adminid'); $querystring = '更新经营设置的短信接口信息设置';
			        $ip = get_ip();$os = get_os();
			        set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
			        return ['code'=>0, 'msg'=>lang('更新成功！')];
	    			//$this->redirect('index');
            }else{
            	return ['code'=>-1050, 'msg'=>lang('更新失败！')];
                //$this->error('修改失败！');
            }
	    	return;	

         }

		return $this->fetch();
	}

	//添加基本信息
	public function add(){

		if (Request::instance()->isPost() ){
			if($_FILES['logo']['name']){
				$file = Request::instance()->file('logo');
				$info=$file->move(ROOT_PATH . 'public' . DS . 'uploads');
				$file = $info->getSaveName();
		        $data['logo'] = '/uploads/'.$file;
			}
			$data['types'] = 1;
			$prepaidamount = input('post.prepaidamount/a',[]);
			$s2 = implode('|',$prepaidamount);
			$data['name'] = input('name');
			$data['status'] = 1;
			$data['contacts'] = input('contacts');
			$data['mobile'] = input('mobile');
			$data['servicephone'] = input('servicephone');
			$data['introduce'] = input('introduce');
			$data['domain'] = input('domain');
			$data['addrs'] = input('addrs');
			$data['deposit'] = input('deposit');
			$data['amount'] = input('amount');
			$data['billingunit'] = input('billingunit');
			$data['billingtime'] = input('billingtime');
			$data['depositrefund'] = input('depositrefund');
			$data['prepaidamount'] = $s2;
			$data['create_time'] =time();
			$data['is_delete'] = 1;
			$xy['xy1'] = input('xy1');
			$xy['xy2'] = input('xy2');
			$xy['xy3'] = input('xy3');
			$xy['xy4'] = input('xy4');
			$xy['oid'] = 1;

			$validate = \think\Loader::validate('Operator');
			if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }

            if(Db::name('operator')->insert($data)){
               if(Db::name( 'agreement')->insert($xy)){
               	$oid = 1;$m = 'ausadmin'; $c = 'Jysetting'; $action="add"; $uid=session('adminid'); $querystring = '添加基本信息设置';
			        $ip = get_ip();$os = get_os();
			        set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
	    			$this->redirect('index');
	    		}
            }else{
                $this->error('添加失败！');
            }

		}	
		return $this->fetch();
	}
	//添加微信支付信息
	public function addpay(){
		$id = 1;
		$path = '../extend/wxpay/cert/'.$id.'/';
        if (Request::instance()->isPost() ){		
			$data['sslcert_path'] = input('sslcert_path');
			$data['sslkey_path'] = input('sslkey_path');
			$data['oid'] = $id;
			$data['mchid'] = input('mchid');
			$data['mchkey'] = input('mchkey');
			$data['submerchant'] = input('submerchant');
	        $validate = \think\Loader::validate('Wechatpay'); 	
	        if(!$validate->scene('addpay')->check($data)){
                $this->error($validate->getError());
            }


            if(Db::name('wechatpay')->insert($data)){
            	$oid = 1;$m = 'ausadmin'; $c = 'Jysetting'; $action="add"; $uid=session('adminid'); $querystring = '添加支付相关信息';
			        $ip = get_ip();$os = get_os();
			        set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
	    			$this->redirect('index');
            }else{
                $this->error('添加失败！');
            }
		}
    }

    //添加短信接口信息
    public function smsadd(){
    	if (Request::instance()->isPost() ){
    		$data = input('post.');
    		$data['oid'] = 1;
    		$validate = \think\Loader::validate('Verificationcode'); 
    		if(!$validate->scene('smsadd')->check($data)){
                $this->error($validate->getError());
            }
            if(Db::name('verificationcode')->insert($data)){
            	$oid = 1;$m = 'ausadmin'; $c = 'Jysetting'; $action="smsadd"; $uid=session('adminid'); $querystring = '添加短信接口有关信息';
			        $ip = get_ip();$os = get_os();
			        set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
	    			$this->redirect('index');
            }else{
                $this->error('添加失败！');
            }
    	}
    }


    public function renew(){
    	if (Request::instance()->isPost() ){

    	}else{
    		$ret = Db::name('operator_order')
    			->alias('a')
    			->join("version b",'a.version = b.id')
    			->join("operator c",'a.oid = c.id')
    			->where('a.is_delete',1)
    			->field('a.*,b.name as vname,b.vision,b.price,c.name as oname')
    			->order('create_time desc')
    			->find();
    		$ver = Db::name('version')->where('id',$ret['version'])->find();
    		$this->assign('ver',$ver);
    		$this->assign('v',$ret);
    		//历史订单记录
    		$list = Db::name('operator_order')
    			  ->alias('a')
    			  ->join("version b",'a.version = b.id')
    			  ->where('a.is_delete',1)
    			  ->where('a.pay_status',1)
    			  ->order('a.create_time desc')
    			  ->field('a.*,b.name as vname,b.vision')
    			  ->select();
    		$this->assign('list',$list);
    		
    		return view();
    	}
    	
    }

    public function version(){
    	$id = Request::instance()->get('id','','number_int');
    	if($id){
 			$result = Db::name('version')->where('id',$id)->find();
 			return $result;
 		}

    }

    /**
     *小程序设置
     *
     */
    public function smallapp()
    {

        return $this->fetch();
    }


}